﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.OleDb;
using libConn;

namespace ApartmentSale
{
	public partial class ManagerInput : Window
	{
		public ManagerInput()
		{
			this.InitializeComponent();

            opCommand = OpCommand.Create;
            gData = new ManagerView();
		}

        public ManagerInput(ManagerView data)
            :this()
        {
            opCommand = OpCommand.Update;

            gData = data;
            tbName.Text = data.manager_name;
            tbRule.Text = data.manager_rebate_rate.ToString("0.000");
        }

        private enum OpCommand { Create, Update }
        private OpCommand opCommand;
        private ManagerView gData;

        private void getData(ManagerView data)
        {
            tbName.Text = data.manager_name;
            tbRule.Text = data.manager_rebate_rate.ToString();
        }

        private bool insertData(ManagerView data)
        {
            string errMessage = null;

            string str = "INSERT INTO manager (manager_name, manager_rebate_rate) VALUES ('";
            str += data.manager_name.Replace("'", "''") + "', ";
            str += data.manager_rebate_rate + ")";

            libConn.AccdbConn.exec(str, ref errMessage);

            if (errMessage != null)
            {
                System.Windows.MessageBox.Show(
                    errMessage,
                    "Save data failed manager discount",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);
                return false;
            }

            MessageBox.Show("Add Manager discount success.");
            return true;
        }

        private bool updateData(ManagerView data)
        {
            string errMessage = null;

            string str = "UPDATE manager SET manager_name = '" + data.manager_name.Replace("'", "''") + "', ";
            str += "manager_rebate_rate = " + data.manager_rebate_rate + " ";
            str += "WHERE manager_id = " + data.manager_id;

            libConn.AccdbConn.exec(str, ref errMessage);

            if (errMessage != null)
            {
                System.Windows.MessageBox.Show(
                    errMessage,
                    "Managers failed to modify the information",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);
                return false;
            }

            return true;
        }

        private void btOk_Click(object sender, RoutedEventArgs e)
        {
            if (tbName.Text.Length == 0)
            {
                MessageBox.Show("Please enter the manager title.");
                tbName.Focus();
                return;
            }

            gData.manager_name = tbName.Text;

            double rule;
            if (tbRule.Text.Length == 0 || !double.TryParse(tbRule.Text, out rule) || rule <= 0 || rule >= 1)
            {
                MessageBox.Show("Please enter a valid number (discount must be between 0.001 and 0.999).");
                tbRule.Focus();
                return;
            }

            gData.manager_rebate_rate = rule;

            if (opCommand == OpCommand.Create)
            {
                if (insertData(gData))
                {
                    this.Close();
                }
            }
            else
            {
                if (updateData(gData))
                {
                    this.Close();
                }
            }
        }

        private void btCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
	}
}